##########################################
## Copyright 2012-2013 Ceruti Francesco
##
##
## This file is part of LiSP (Linux Show Player).
##
## LiSP (Linux Show Player) is free software: you can redistribute it and/or modify
## it under the terms of the GNU Lesser General Public License as published by
## the Free Software Foundation, either version 3 of the License, or
## (at your option) any later version.
##
## LiSP (Linux Show Player) is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU Lesser General Public License for more details.
##
## You should have received a copy of the GNU Lesser General Public License
## along with LiSP (Linux Show Player).  If not, see <http://www.gnu.org/licenses/>.
##########################################

from PyQt4 import QtCore
from PyQt4 import QtGui
from lisp.ui.preferences.preferences_widget import PreferencesWidget


class Equalizer(PreferencesWidget):

    NAME = 'Equalizer'

    FREQ = ['29Hz', '59Hz', '119Hz', '237Hz', '474Hz', '947Hz', '1889Hz', '3770Hz', '7523Hz', '15011Hz']

    def __init__(self, size, parent=None):
        super(Equalizer, self).__init__(size, parent)

        self.groupBox = QtGui.QGroupBox(self)
        self.groupBox.setTitle('10 Bands Equalizer')

        self.verticalLayoutWidget = QtGui.QWidget(self.groupBox)
        self.verticalLayoutWidget.setGeometry(QtCore.QRect(80, 25, 431, 430))
        self.verticalLayout = QtGui.QVBoxLayout(self.verticalLayoutWidget)
        self.verticalLayout.setMargin(0)

        self.verticalLayoutWidget_2 = QtGui.QWidget(self.groupBox)
        self.verticalLayoutWidget_2.setGeometry(QtCore.QRect(0, 25, 80, 430))
        self.verticalLayout_2 = QtGui.QVBoxLayout(self.verticalLayoutWidget_2)
        self.verticalLayout_2.setMargin(0)

        self.sliders = {}

        for n in range(10):
            horizontalLayoutWidget = QtGui.QWidget(self.verticalLayoutWidget)
            horizontalLayout = QtGui.QHBoxLayout(horizontalLayoutWidget)
            horizontalLayout.setMargin(0)

            fLabel = QtGui.QLabel(self.verticalLayoutWidget_2)
            fLabel.setText(self.FREQ[n])
            self.verticalLayout_2.addWidget(fLabel)

            horizontalSlider = QtGui.QSlider(horizontalLayoutWidget)
            sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Preferred, QtGui.QSizePolicy.Fixed)
            horizontalSlider.setSizePolicy(sizePolicy)
            horizontalSlider.setMinimum(-24)
            horizontalSlider.setMaximum(12)
            horizontalSlider.setPageStep(1)
            horizontalSlider.setProperty("value", 0)
            horizontalSlider.setOrientation(QtCore.Qt.Horizontal)
            self.sliders['band' + str(n)] = horizontalSlider

            horizontalLayout.addWidget(horizontalSlider)

            label = QtGui.QLabel(horizontalLayoutWidget)
            label.setAlignment(QtCore.Qt.AlignCenter)
            label.setNum(0)
            horizontalLayout.addWidget(label)

            self.verticalLayout.addWidget(horizontalLayoutWidget)

            horizontalSlider.valueChanged.connect(label.setNum)

    def enableCheck(self, enable):

        self.groupBox.setCheckable(enable)
        self.groupBox.setChecked(False)

    def getConfiguration(self):
        conf = {}
        if(self.groupBox.isCheckable() and not self.groupBox.isChecked()):
            conf['equalizer'] = {}
            for band in self.sliders:
                conf['equalizer'][band] = self.sliders[band].value()
        return conf

    def setConfiguration(self, conf):
        if(conf is not None):
            if('equalizer' in conf):
                if(type(conf['equalizer']) == type({})):
                    for band in self.sliders:
                        if(band in conf['equalizer']):
                            self.sliders[band].setValue(conf['equalizer'][band])
                        else:
                            self.sliders[band].setEnabled(False)
                else:
                    self.setEnabled(False)
            else:
                self.setEnabled(False)
